package shoppingmall.manage.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shoppingmall.manage.pojo.Commodity;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.dto.Result;
import shoppingmall.manage.service.CommodityService;

import java.util.List;

/**
 * 商品管理相关controller
 */
@Slf4j
@RestController
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    /**
     * 分页查询商品(默认查询第一页十条数据)
     */
    @GetMapping("/manage/commodity")
    public Result getCommodityList (@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        log.info("查询商品,页码:{},条数:{}",page,pageSize);
        PageBean pageBean = commodityService.getCommodityList(page, pageSize);
        if(pageBean.getTotal() == 0) {
            return Result.error("未查询到商品数据!");
        }
        return Result.success(pageBean);
    }

    /**
     * 查询所有的商品(数据大屏要显示平台商品总数)
     */
    @GetMapping("/manage/commodity/all")
    public Result getAllCommodityList () {
        log.info("查询平台所有商品数据!");
        //调用服务层方法
        PageBean allCommodityList = commodityService.getAllCommodityList();
        if(allCommodityList.getTotal() == 0) {
            return Result.success("未查询到商品信息!");
        }
        return Result.success(allCommodityList);
    }

    /**
     * 添加一条商品数据
     */
    @PostMapping("/manage/commodity")
    public Result saveCommodity (@RequestBody Commodity commodity) {
        log.info("添加一条商品数据:{}",commodity.getCommodityName());
        boolean isSave = commodityService.saveCommodity(commodity);
        if(!isSave){
            return Result.error("商品数据添加失败!");
        }
        return Result.success("商品数据添加成功!");
    }

    /**
     * 修改一条商品数据
     */
    @PutMapping("/manage/commodity")
    public Result updateCommodity (@RequestBody Commodity commodity) {
        log.info("修改一条商品数据,id为:{}",commodity.getCommodityId());
        //调用服务层方法
        boolean isUpdate = commodityService.updateCommodity(commodity);
        if(!isUpdate){
            return Result.error("商品数据修改失败!");
        }
        return Result.success("商品数据修改成功!");
    }

    /**
     * 删除一条商品数据
     */
    @DeleteMapping("/manage/commodity")
    public Result deleteCommodity (@RequestParam int commodityId) {
        log.info("删除一条商品数据,id为:{}",commodityId);
        //调用服务层方法
        boolean isDel = commodityService.deleteCommodity(commodityId);
        if(!isDel){
            return Result.error("商品删除失败!");
        }
        return Result.success("商品删除成功!");
    }

    /**
     * 根据商品分类对商品进行筛选
     */
    @GetMapping("/manage/commodity/category")
    public Result getCommodityListByCategory (@RequestParam int commodityCategoryId) {
        log.info("分类查询商品,分类id:{}",commodityCategoryId);
        PageBean pageBean = commodityService.getCommodityListByCategory(commodityCategoryId);
        if(pageBean.getTotal() == 0) {
            return Result.error("未查询到商品数据!");
        }
        return Result.success(pageBean);
    }

    /**
     * 批量删除商品数据 根据商品id commodityIds
     */
    @DeleteMapping("/manage/commodity/batch")
    public Result deleteCommodityBatch (@RequestBody List<Integer> commodityIds) {
        log.info("批量删除商品数据,commodity_id={}",commodityIds.toString());
        boolean isDel = commodityService.deleteCommodityBatch(commodityIds);
        if(!isDel) {
            return Result.error("批量删除商品失败!");
        }
        return Result.success("批量删除商品成功!");
    }

    /**
     * 根据商品名称对商品进行模糊搜索
     */
    @GetMapping("/manage/commodity/like")
    public Result getCommodityListByCommodityName (@RequestParam String commodityName) {
        log.info("根据商品名称对商品数据进行模糊搜索,commodity_name = {}",commodityName);
        PageBean pageBean = commodityService.getCommodityListByCommodityName(commodityName);
        if(pageBean.getTotal() == 0) {
            return Result.error("未查询到商品数据!");
        }
        return Result.success(pageBean);
    }

    /**
     * 根据某个字段对商品进行排序输出
     * fieldName = commodity_sales || commodity_sale_money    sortRule = asc || desc
     */
    @GetMapping("/manage/commodity/sort")
    public Result getCommodityListSort (@RequestParam String fieldName, String sortRule) {
        log.info("根据某字段某种排序方式对商品表排序输出,fieldName = {}, sortRule = {}",fieldName,sortRule);
        PageBean pageBean = commodityService.getCommodityListSort(fieldName,sortRule);
        if(pageBean.getTotal() == 0) {
            return Result.error("未查询到商品数据!");
        }
        return Result.success(pageBean);
    }
}
